package com.leetcode.easy;

import com.leetcode.entity.TreeNode;

/**
 * @Classname IsBalanced
 * @Description 判断一个二叉树是否是平衡二叉树
 * @Date 2020/9/18 9:53
 * @Created by zfl
 */
public class IsBalanced {

    public boolean isBalanced(TreeNode root) {
        return height(root) > 1 ? false : true;
    }

    public int height(TreeNode root) {
        if(root == null) {
            return 0;
        }
        TreeNode left = root.left;
        TreeNode right = root.right;
        int leftDeepth = Math.max(height(left.left), height(left.right)) + 1;
        int rightDeepth = Math.max(height(right.left), height(right.right)) + 1;
        return rightDeepth - leftDeepth;
    }
}
